<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="日统计号" prop="dayExitStatisticNumber">
        <el-input
          v-model="queryParams.dayExitStatisticNumber"
          placeholder="请输入日统计号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出库日期" prop="exitWarehouseDate">
        <el-date-picker clearable
          v-model="queryParams.exitWarehouseDate"
          style="width: 205px"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择出库日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="出库客户" prop="exitWarehouseCustomer">
        <el-select v-model="queryParams.exitWarehouseCustomer" placeholder="请选择出库客户" clearable filterable>
          <el-option
            v-for="dict in customerOptions"
            :key="dict.customerId"
            :label="dict.customerName"
            :value="dict.customerName"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="货物类型" prop="goodsType">
        <el-select v-model="queryParams.goodsType" placeholder="请选择货物类型" clearable>
          <el-option
            v-for="dict in dict.type.goods_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.label"
          />
        </el-select>
      </el-form-item>
      <!--el-form-item label="计重方式" prop="weightMode">
        <el-select v-model="queryParams.weightMode" placeholder="请选择计重方式" clearable>
          <el-option
            v-for="dict in dict.type.weight_mode"
            :key="dict.value"
            :label="dict.label"
            :value="dict.label"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="日出库量" prop="dayExitWarehouseWeight">
        <el-input
          v-model="queryParams.dayExitWarehouseWeight"
          placeholder="请输入日出库量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="理/磅重量" prop="dayExitWarehouseTheoryWeight">
        <el-input
          v-model="queryParams.dayExitWarehouseTheoryWeight"
          placeholder="请输入理/磅重量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="queryParams.remark"
          placeholder="请输入备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="园区代码" prop="parkCode">
        <el-input
          v-model="queryParams.parkCode"
          placeholder="请输入园区代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建者" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeCreateTime"
          style="width: 205px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item-->
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['statistic:dayexit:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['statistic:dayexit:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['statistic:dayexit:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['statistic:dayexit:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" @computeTable="computeTableHeight"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dayexitList" @selection-change="handleSelectionChange" border :height="tableHeight" :row-style="{height:'35px'}" :cell-style="{padding:'0px'}">
      <el-table-column type="selection" width="45" align="center" />
      <el-table-column type="index" prop="" width="55" label="序号" align="center" >
        <template slot-scope="scope">
          <span>{{ scope.$index+1 +(queryParams.pageNum-1)*queryParams.pageSize }}</span>
        </template>
      </el-table-column>
      <!--el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100%">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['statistic:dayexit:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['statistic:dayexit:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="handleGoods(scope.row)"
            
          >货物</el-button>
        </template>
      </el-table-column-->
      <!--el-table-column label="统计ID" align="center" prop="dayExitStatisticId" /-->
      <el-table-column label="日统计号" align="center" prop="dayExitStatisticNumber" width="160" :show-overflow-tooltip="true" >
        <template slot-scope="scope">
          <router-link :to="'/statistic/dayexit-relategoods/index/' + scope.row.dayExitStatisticId" class="link-type">
            <span>{{ scope.row.dayExitStatisticNumber }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="出库日期" align="center" prop="exitWarehouseDate" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.exitWarehouseDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="出库客户" align="center" prop="exitWarehouseCustomer" width="240" :show-overflow-tooltip="true" />
      <el-table-column label="货物类型" align="center" prop="goodsType" />
      <el-table-column label="计重方式" align="center" prop="weightMode" />
      <el-table-column label="日出库量" align="center" prop="dayExitWarehouseWeight" />
      <el-table-column label="理/磅重量" align="center" prop="dayExitWarehouseTheoryWeight" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建者" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改日出库量对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="日统计号" prop="dayExitStatisticNumber">
          <el-input v-model="form.dayExitStatisticNumber" placeholder="请输入日统计号" clearable />
        </el-form-item>
        <el-form-item label="出库日期" prop="exitWarehouseDate">
          <el-date-picker clearable
            v-model="form.exitWarehouseDate"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择出库日期"
            style="width: 340px;">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="出库客户" prop="exitWarehouseCustomer">
          <el-select v-model="form.exitWarehouseCustomer" placeholder="请选择出库客户" style="width: 340px;" clearable>
            <el-option
              v-for="dict in customerOptions"
              :key="dict.customerId"
              :label="dict.customerName"
              :value="dict.customerName"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="货物类型" prop="goodsType">
          <el-select v-model="form.goodsType" placeholder="请选择货物类型" style="width: 340px;" clearable>
            <el-option
              v-for="dict in dict.type.goods_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="计重方式" prop="weightMode">
          <el-select v-model="form.weightMode" placeholder="请选择计重方式" style="width: 340px;" clearable>
            <el-option
              v-for="dict in dict.type.weight_mode"
              :key="dict.value"
              :label="dict.label"
              :value="dict.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="日出库量" prop="dayExitWarehouseWeight">
          <el-input v-model="form.dayExitWarehouseWeight" placeholder="请输入日出库量" clearable />
        </el-form-item>
        <el-form-item label="理/磅重量" prop="dayExitWarehouseTheoryWeight">
          <el-input v-model="form.dayExitWarehouseTheoryWeight" placeholder="请输入理/磅重量" clearable />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" clearable />
        </el-form-item>
        <el-form-item label="园区代码" prop="parkCode">
          <el-input v-model="form.parkCode" placeholder="请输入园区代码" clearable />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDayexit, getDayexit, delDayexit, addDayexit, updateDayexit } from "@/api/statistic/dayexit";
import { listCustomerOption } from "@/api/customs/custom";

export default {
  name: "Dayexit",
  dicts: ['sys_yes_no', 'weight_mode', 'goods_type'],
  data() {
    return {
      // 定义列表高度
      tableHeight: 430,
      // 定义查询高度
      queryHeight: 200,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 日出库量表格数据
      dayexitList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 园区代码时间范围
      daterangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dayExitStatisticNumber: null,
        exitWarehouseDate: null,
        exitWarehouseCustomer: null,
        goodsType: null,
        weightMode: null,
        dayExitWarehouseWeight: null,
        dayExitWarehouseTheoryWeight: null,
        remark: null,
        parkCode: null,
        createBy: null,
        createTime: null,
      },
      // 表单参数
      form: {},
      // 字典项客户
      customerOptions: [],
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getOption();
  },
  mounted() {
    this.$nextTick(() =>{
      // 根据浏览器高度设置初始高度 - 按钮行高度28 - 页码行高度28 - （面包屑+TAB行）高度160
      this.queryHeight = this.$refs.queryForm.$el.offsetHeight;
      this.tableHeight = window.innerHeight - this.$refs.queryForm.$el.offsetHeight - 28 - 28 - 160;
      // 监听浏览器高度变化，改变表格高度
      window.onresize = () =>{
        this.tableHeight = window.innerHeight - this.$refs.queryForm.$el.offsetHeight - 28 - 28 - 160;
      }
    })
  },
  methods: {
    /** 查询日出库量列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      listDayexit(this.queryParams).then(response => {
        this.dayexitList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        dayExitStatisticId: null,
        dayExitStatisticNumber: null,
        exitWarehouseDate: null,
        exitWarehouseCustomer: null,
        goodsType: null,
        weightMode: null,
        dayExitWarehouseWeight: null,
        dayExitWarehouseTheoryWeight: null,
        remark: null,
        dataRealm: null,
        parkCode: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.dayExitStatisticId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加日出库量";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const dayExitStatisticId = row.dayExitStatisticId || this.ids
      getDayexit(dayExitStatisticId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改日出库量";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.dayExitStatisticId != null) {
            updateDayexit(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDayexit(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const dayExitStatisticIds = row.dayExitStatisticId || this.ids;
      this.$modal.confirm('是否确认删除日出库量编号为"' + dayExitStatisticIds + '"的数据项？').then(function() {
        return delDayexit(dayExitStatisticIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('statistic/dayexit/export', {
        ...this.queryParams
      }, `dayexit_${new Date().getTime()}.xlsx`)
    },
    computeTableHeight(){
      if( this.showSearch==true ){
        this.tableHeight = window.innerHeight - this.queryHeight - 28 - 28 - 160;
      } else {
        this.tableHeight = window.innerHeight - 28 - 28 - 160;
      }
    },
    handleGoods(row) {
      const dayExitStatisticIds = row.dayExitStatisticId || this.ids;
      console.log('dayExitStatisticIds', dayExitStatisticIds);
    },
    getOption() {
      /** 字典项客户*/
      listCustomerOption().then(response => {
        this.customerOptions = response.data;
      });
    },
  }
};
</script>
